<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑用户信息</title>
    <!-- 引入Layui CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.8.17/css/layui.min.css">
    <script src="https://cdn.staticfile.org/layui/2.8.17/layui.min.js"></script>
    <script src="<c:url value="/js/jquery-3.7.1.min.js"/>"></script>
    <script src="../../js/nav.js"></script>
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            color: #333;
        }

        .header {
            background-color: #ffffff;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            padding: 15px 30px;
            display: flex;
            align-items: center;
        }

        .header a {
            color: #333;
            margin-left: 25px;
            text-decoration: none;
            transition: color 0.3s ease;
            font-size: 15px;
        }

        .header a:hover {
            color: #1E9FFF;
        }

        .header > a:first-child {
            margin-left: 0;
        }

        .ml-auto {
            margin-left: auto;
        }

        .admin-container {
            padding: 30px 20px;
            max-width: 1400px;
            margin: 0 auto;
        }

        .edit-user-form {
            background-color: #ffffff;
            border-radius: 16px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
            padding: 30px;
        }

        .form-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-size: 14px;
            font-weight: 500;
            color: #666;
            margin-bottom: 8px;
        }

        .form-group input {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #e0e2e5;
            border-radius: 8px;
            font-size: 14px;
            color: #333;
            transition: border-color 0.2s ease;
        }

        .form-group input:focus {
            border-color: #1E9FFF;
            outline: none;
        }

        .form-group .create-time {
            font-size: 14px;
            color: #666;
        }

        .password-field {
            position: relative;
        }

        .password-field .edit-icon {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: #1E9FFF;
            cursor: pointer;
            opacity: 0.7;
            transition: opacity 0.2s;
        }

        .password-field .edit-icon:hover {
            opacity: 1;
        }

        .password-fields {
            display: none;
        }

        .form-actions {
            display: flex;
            gap: 10px;
            margin-top: 30px;
        }

        .form-actions button {
            background-color: #1E9FFF;
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            font-size: 14px;
            font-weight: 500;
            box-shadow: 0 2px 8px rgba(30, 159, 255, 0.2);
        }

        .form-actions button:hover {
            background-color: #0d87e6;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(30, 159, 255, 0.3);
        }

        .form-actions button i {
            margin-right: 6px;
        }

        /* 新增的密码验证提示样式 */
        .password-error {
            color: #ff4d4f;
            font-size: 12px;
            margin-top: 4px;
            display: none;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .admin-container {
                padding: 20px 15px;
            }

            .edit-user-form {
                padding: 20px;
            }
        }

        #sub {
            display: none;
        }
    </style>
</head>
<body>
<nav class="header">

    <c:if test="${person.level == 1}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
        <a href="${pageContext.request.contextPath}/admin/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">管理员首页</span>
        </a>
    </c:if>
    <c:if test="${person.level == 0}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
    </c:if>

    <div class="ml-auto flex items-center">
        <c:if test="${person == null}">
            <a href="${pageContext.request.contextPath}/views/login.jsp"><i class="fa fa-user-circle mr-2"></i>请先登录</a>
        </c:if>
        <c:if test="${person != null}">
            <a href="${pageContext.request.contextPath}/admin/person/editPerson?pid=${person.pid}"><i
                    class="fa fa-user-circle mr-2"></i>${person.username}</a>
        </c:if>

        <a href="javascript:void(0);" onclick="logout()">退出登录</a>
    </div>
</nav>

<div class="admin-container">
    <div class="edit-user-form">
        <div class="form-title">编辑用户信息</div>
        <form action="${pageContext.request.contextPath}/admin/person/updatePerson" method="post" id="userForm">
            <input type="hidden" name="pid" value="${editPerson.pid}">
            <div class="form-group">
                <label for="username">用户名称</label>
                <input type="text" id="username" name="username" value="${editPerson.username}" required>
            </div>
            <div class="form-group">
                <label for="email">用户邮箱</label>
                <input type="email" id="email" name="email" value="${editPerson.email}" required>
            </div>

            <input type="hidden" name="password" id="password" value="${editPerson.password}">
            <!-- 密码区域 -->
            <div class="form-group">
                <label>用户密码</label>
                <div class="password-field">
                    <input type="text" id="passwordPlaceholder" value="********" readonly>
                    <i class="fa fa-pencil edit-icon" id="editPassword"></i>
                </div>

                <div class="password-fields">
                    <div class="form-group">
                        <label for="oldPassword">当前密码</label>
                        <input type="password" id="oldPassword" name="oldPassword" required>
                    </div>
                    <div class="form-group">
                        <label for="newPassword">新密码</label>
                        <input type="password" id="newPassword" name="newPassword" required>
                        <div class="password-error" id="newPasswordError">密码长度至少为6位，可以包含字母和数字</div>
                    </div>
                    <div class="form-group">
                        <label for="confirmPassword">确认新密码</label>
                        <input type="password" id="confirmPassword" name="confirmPassword" required>
                        <div class="password-error" id="confirmPasswordError">两次输入的密码不一致</div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label>创建时间</label>
                <div class="create-time">${editPerson.date}</div>
            </div>

            <div class="form-actions">
                <button type="button" id="submitBtn"><i class="fa fa-save"></i> 保存</button>
                <button type="button" onclick="goBack()"><i class="fa fa-arrow-left"></i> 返回</button>
                <button type="submit" hidden="hidden" id="sub"></button>
            </div>
        </form>
    </div>
    <p style="color: red">${updatePersonMsg}</p>
</div>

<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 编辑密码功能
        document.getElementById('editPassword').addEventListener('click', function () {
            var passwordFields = document.querySelector('.password-fields');
            passwordFields.style.display = 'block';
            document.getElementById('passwordPlaceholder').style.display = 'none';
        });

        // 密码验证功能
        var newPasswordInput = document.getElementById('newPassword');
        var confirmPasswordInput = document.getElementById('confirmPassword');
        var newPasswordError = document.getElementById('newPasswordError');
        var confirmPasswordError = document.getElementById('confirmPasswordError');

        // 新密码验证
        newPasswordInput.addEventListener('input', function () {
            validatePassword();
        });

        // 确认密码验证
        confirmPasswordInput.addEventListener('input', function () {
            validatePassword();
        });

        function validatePassword() {
            var newPassword = newPasswordInput.value;
            var confirmPassword = confirmPasswordInput.value;
            var isValid = true;

            // 只有在密码字段可见时才验证旧密码
            if (document.querySelector('.password-fields').style.display === 'block') {
                // 判断旧密码输入是否正确
                var oldPwd = $('#oldPassword').val();
                var password = $('#password').val();
                if (oldPwd !== password) {
                    layer.msg('旧密码输入错误', {icon: 5});
                    isValid = false;
                    return isValid;
                }
            }

            // 验证新密码强度
            if (newPassword.length < 6 || !/[0-9]/.test(newPassword)) {
                newPasswordError.style.display = 'block';
                isValid = false;
            } else {
                newPasswordError.style.display = 'none';
            }

            // 验证两次输入是否一致
            if (newPassword !== confirmPassword && confirmPassword.length > 0) {
                confirmPasswordError.style.display = 'block';
                isValid = false;
            } else {
                confirmPasswordError.style.display = 'none';
            }

            return isValid;
        }

        // 修改为点击按钮而非提交表单时处理
        document.getElementById('submitBtn').addEventListener('click', function () {
            // 先检查基本表单验证
            var isValid = true;
            $('#userForm input[required]').each(function () {
                // 只验证可见的必填字段
                if ($(this).is(':visible') && !$(this).val()) {
                    isValid = false;
                    layer.msg('请填写所有必填字段', {icon: 5});
                    $(this).focus();
                    return false; // 终止each循环
                }
            });

            if (!isValid) return;

            // 如果密码字段可见，验证密码
            if (document.querySelector('.password-fields').style.display === 'block') {
                if (!validatePassword()) {
                    layer.msg('密码格式不正确', {icon: 5});
                    return;
                }

                // 修改密码验证正确
                let newPassword = $('input[name="newPassword"]').val();
                if (newPassword !== '') {
                    $('#password').val(newPassword);
                }
            } else {
                // 如果密码字段未修改，则不提交密码相关字段
                $('input[name="oldPassword"]').removeAttr('required');
                $('input[name="newPassword"]').removeAttr('required');
                $('input[name="confirmPassword"]').removeAttr('required');
            }

            // 显示加载动画
            var loading = layer.load(1, {
                shade: [0.1, '#fff']
            });

            // 模拟表单提交（实际项目中这里应该是AJAX请求）
            setTimeout(function () {
                layer.close(loading); // 关闭加载动画
                $('#sub').click();
                // 显示成功消息
                layer.msg('用户信息更新成功', {
                    icon: 1,
                    time: 2000 // 2秒后自动关闭
                }, function () {
                    // 成功提示关闭后返回上一页
                    window.history.back();
                });
            }, 800); // 模拟0.8秒的处理时间
        });
    });

    function logout() {
        layer.confirm('确定要退出登录吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功退出登录', {icon: 1});
            setTimeout(function () {
                window.location.href = '/VoingSystem/logout';
            }, 1500);
            layer.close(index);
        })
    }

    function goBack() {
        layer.confirm('确定不保存返回吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('修改信息未保存', {icon: 1});
            setTimeout(function () {
                window.history.back();
            }, 1500);
            layer.close(index);
        })
    }

    function noGoEdit() {
        layer.msg('当前页面已经是用户信息修改页面了', {icon: 5})
    }
</script>
</body>
</html>